package com.example.peibei.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.PaidanList2Result;
import com.dingtao.common.bean.PaidanList3;
import com.dingtao.common.bean.PaidanListResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.PaidanApplyCancelPresenter;
import com.example.peibei.service.presenter.ServiceOrderListPresenter;
import com.example.peibei.ui.adapter.PaidanLists2Adapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.Random;

import butterknife.BindView;

public class PaidanList2Fragment extends WDFragment {
    @BindView(R.id.rv_bill)
    RecyclerView rv_bill;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    PaidanLists2Adapter adapter;
    ServiceOrderListPresenter consumerOrderListPresenter;
    PaidanApplyCancelPresenter paidanApplyCancelPresenter;
    SPUtils spUtils;
    String token;
    String type;

    public PaidanList2Fragment setType(String type) {
        this.type = type;
        return this;
    }

    @Override
    public String getPageName() {
        return "list1"+ new Random().nextInt();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_paidan_list1;
    }

    @Override
    protected void initView() {
        adapter = new PaidanLists2Adapter();
        rv_bill.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_bill.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.tv_cancel);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter a, @NonNull View view, int position) {
                PaidanList3 item = adapter.getItem(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("取消接单");
                builder.setMessage("确定取消接单吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        adapter.remove(item);
                        paidanApplyCancelPresenter.reqeust("Bearer " + token,item.getId());
                    }
                });
                builder.setNegativeButton("再想想",null);
                builder.show();
            }
        });

        refreshLayout.setOnRefreshListener(refreshLayout -> onRefreshStart());
        refreshLayout.setOnLoadMoreListener(refreshLayout1 -> onLoadMore());

        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);

        consumerOrderListPresenter = new ServiceOrderListPresenter(new ConsumerOrderCall());
        paidanApplyCancelPresenter = new PaidanApplyCancelPresenter(new ApplyCancelCall());
        onRefreshStart();
    }

    public void onRefreshStart(){
        consumerOrderListPresenter.reqeust("Bearer " + token,type,true);
    }

    public void onLoadMore(){
        consumerOrderListPresenter.reqeust("Bearer " + token,type,false);
    }

    public void finishRefLoad(){
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    class ConsumerOrderCall implements DataCall<PaidanList2Result> {

        @Override
        public void success(PaidanList2Result data, Object... args) {
            finishRefLoad();
            if (consumerOrderListPresenter.isRefresh()){
                adapter.setNewInstance(data.getRecords());
            }else {
                adapter.addData(data.getRecords());
            }
            if (adapter.getData().size() > 0){
                EventBus.getDefault().post("paidan_type_2");
            }
            if (consumerOrderListPresenter.getPage() >= data.getTotal()){
                refreshLayout.setEnableLoadMore(false);
            }else {
                refreshLayout.setEnableLoadMore(true);
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
            finishRefLoad();
        }
    }
    class ApplyCancelCall implements DataCall<Object> {

        @Override
        public void success(Object data, Object... args) {

        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
